﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace mitasoft.ERS.Web.Core
{
    public class CacheEngine
    {
        /// <summary>
        /// Check key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static bool ExistsKey(string key)
        {
            return HttpContext.Current.Cache[key] != null;
        }

        /// <summary>
        /// Add to cache
        /// </summary>
        /// <param name="key"></param>
        /// <param name="data"></param>
        public static void AddToCache(string key, object data)
        {
            if (!ExistsKey(key))
                HttpContext.Current.Cache.Insert(key, data, null);
        }

        /// <summary>
        /// Get from cache
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetFromCache<T>(string key)
        {
            if (ExistsKey(key))
                return (T)HttpContext.Current.Cache[key];

            return default(T);
        }
    }
}